Move loading the agent instance to a helper

Also rescue when the agents haven't been loaded yet into memory. (Per @dsander &
@knu comments)

Colin Shea 9 years ago
parent
commit
3ca2404f63
2 changed files with 15 additions and 1 deletions
  1. 14 0
      app/helpers/jobs_helper.rb
  2. 1 1
      app/views/jobs/index.html.erb

+ 14 - 0
app/helpers/jobs_helper.rb

@@ -18,4 +18,18 @@ module JobsHelper
18 18
       'in ' + distance_of_time_in_words(time, now)
19 19
     end
20 20
   end
21
+
22
+  # Given an queued job, parse the stored YAML to retrieve the ID of the Agent
23
+  # meant to be ran.
24
+  #
25
+  # Can return nil, or an instance of Agent.
26
+  def agent_from_job(job)
27
+    begin
28
+      Agent.find_by_id(YAML.load(job.handler).args[0])
29
+    rescue ArgumentError
30
+      # We can get to this point before all of the agents have loaded (usually,
31
+      # in development)
32
+      nil
33
+    end
34
+  end
21 35
 end

+ 1 - 1
app/views/jobs/index.html.erb

@@ -20,7 +20,7 @@
20 20
           </tr>
21 21
 
22 22
         <% @jobs.each do |job| %>
23
-          <% agent = Agent.find_by_id(YAML.load(job.handler).args[0]) %>
23
+          <% agent = agent_from_job(job) %>
24 24
           <tr>
25 25
             <td><%= status(job) %></td>
26 26
             <td><%= agent ? link_to(agent.name, agent_path(agent)) : "(deleted)" %></td>